const _  =  require('lodash');
var mongoose = require('mongoose');
require('../models/client.model.js');
var merchantModel = mongoose.model('Merchant');

module.exports = {
    merchantRegister: merchantRegister,
    getMerchantByLogin: getMerchantByLogin,
    getMerchantByIcnum: getMerchantByIcnum,
    merchantUpdate: merchantUpdate,
}

function merchantRegister(req, res, next){
    let merchant = req.body;
    merchantModel.create(merchant, function(err, result) {
        if (err) {
            return next(new Error(err));
        } else {
            return res.json(result);
        }  
    })
}

function getMerchantByLogin(req, res, next){
    merchantModel.find({"operatStatus":1,"status1":1,"status2":1},function(err, result){
        if (err) {
            return next(new Error(err));
        } else {
            console.log("merchants..............!!!!!!!!!!!")
            return res.json(result);
        }  
    })
}

function getMerchantByIcnum(req, res, next){
    let params = req.params;
    let icnum = params.icnum;
    console.log("xxxxx"+icnum);
    merchantModel.findOne({'icnum': icnum}, function(err, result){
        if (err) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }     
    })

}


function merchantUpdate(req, res, next){
    let merchant = req.body;
    merchantModel.findByIdAndUpdate({"_id":id},{
        $set:merchant
    },function(err, result){
        if (error) {
            return next(new Error(error));
        } else {
            return res.json(result);
        }    
    })


}